using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
namespace Bamboo.Core.Data
{
    public class SearchQueryHelper
    {
        public static SearchOperator ConvertToOperator(string operatorString)
        {
            if (operatorString == "like")
            {
                return SearchOperator.LIKE;
            }
            else if (operatorString == "=")
            {
                return SearchOperator.EQUAL;
            }
            else if (operatorString == ">")
            {
                return SearchOperator.GREATER;
            }
            else if (operatorString == ">=")
            {
                return SearchOperator.GREATER_EQUAL;
            }
            else if (operatorString == "<")
            {
                return SearchOperator.SMALLER;
            }
            else if (operatorString == "<=")
            {
                return SearchOperator.SMALLER_EQUAL;
            }
            return SearchOperator.EQUAL;

        }

        public static string GetOperatorString(SearchOperator op) {
            switch (op)
            {
                case SearchOperator.LIKE:
                    return "like";
                case SearchOperator.GREATER:
                    return ">";
                case SearchOperator.GREATER_EQUAL:
                    return ">=";
                case SearchOperator.SMALLER:
                    return "<";
                case SearchOperator.SMALLER_EQUAL:
                    return "<=";
                case SearchOperator.EQUAL:
                    return "=";
                case SearchOperator.IS:
                    return "is";

                default:
                    break;
            }
            return null;
        }

        public static string BuildFullSearchHQL(string className, IList<SearchCondition> searchConditionList)
        {
            if (className == null || searchConditionList == null)
            {
                return null;
            }
            StringBuilder sbQuery = new StringBuilder();
            sbQuery.Append("select item from " + className + " item where 1 = 1 ");
            string conditionQuery = BuildSearchHQL(searchConditionList);
            if(conditionQuery != null) 
            {
                sbQuery.Append(" and " + conditionQuery);
            }
            
            return sbQuery.ToString();

        }

        public static string BuildSearchHQL(IList<SearchCondition> searchConditionList)
        {
            if (searchConditionList == null || searchConditionList.Count == 0)
            {
                return null;
            }
            StringBuilder sbQuery = new StringBuilder();
            for (int i = 0; i < searchConditionList.Count; i++)
            {
                SearchCondition searchCondition = searchConditionList[i] as SearchCondition;
                sbQuery.Append(" item." + searchCondition.SearchField);
                sbQuery.Append(" " + GetOperatorString(searchCondition.SearchOperator) + " ");
                sbQuery.Append(":" + searchCondition.SearchParameterName);
                if (i != searchConditionList.Count - 1)
                {
                    sbQuery.Append(" and ");
                } 
            }
            return sbQuery.ToString();
        }

        public static void SetupSearchQuery(IList<SearchCondition> searchConditionList, IQuery query)
        {
            if (searchConditionList == null || query == null)
            {
                return;
            }
            foreach (SearchCondition condition in searchConditionList)
            {
                query.SetParameter(condition.SearchParameterName, condition.SearchParameterValue);
            }
        }
    }
}
